import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/gate_equipment_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/gate_region_entity.dart';

/// 门禁授权列表页面
class AccessControlWidget extends StatefulWidget {
  const AccessControlWidget({required this.selectedRegion, required this.selectedAccessControl, required this.equipmentType, super.key});

  /// 选中的区域
  final List<GateRegionEntity> selectedRegion;

  /// 选中的门禁
  final List<GateEquipmentEntity> selectedAccessControl;

  /// 设备类型 1:人员闸机 2:车辆闸机
  final String equipmentType;

  @override
  State<AccessControlWidget> createState() => _AccessControlWidgetState();
}

class _AccessControlWidgetState extends State<AccessControlWidget> {
  @override
  Widget build(BuildContext context) {
    return CustomEasyRefreshList<GateEquipmentEntity>(
      isShowSearch: true,
      searchHint: '请输入门禁名称',
      enableLoad: false,
      dataLoad: (pageIndex, pageSize, {String? search}) async {
        return _getListData(pageIndex, pageSize, search: search);
      },
      itemBuilder: (GateEquipmentEntity item, int index) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(8.w)),
            color: const Color(0xffEEF0FF),
          ),
          child: Column(children: [
            Row(
              children: [
                Expanded(
                    child: Text(
                  item.deviceName,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xff5C6DFF),
                    fontWeight: FontWeight.w500,
                  ),
                )),
                SvgPicture.asset(
                  widget.selectedAccessControl.any((obj) => obj.id == item.id) ? AppImages.icon_selection_box : AppImages.icon_un_selection_box,
                  width: 16.w,
                  height: 16.w,
                ),
              ],
            ),
            AppGaps.vGap10,
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '区域名称：',
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.2,
                    color: const Color(0xffA0A7B5),
                    fontWeight: FontWeight.w400,
                  ),
                ),
                Expanded(
                    child: Text(
                  item.regionName,
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.2,
                    color: const Color(0xff495061),
                    fontWeight: FontWeight.w400,
                  ),
                )),
              ],
            ),
            AppGaps.vGap10,
            Row(
              children: [
                Text(
                  '已经分配人数：',
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.2,
                    color: const Color(0xffA0A7B5),
                    fontWeight: FontWeight.w400,
                  ),
                ),
                Expanded(
                    child: Text(
                  item.allocatedQuantity.toString(),
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.2,
                    color: const Color(0xff495061),
                    fontWeight: FontWeight.w400,
                  ),
                )),
                Text(
                  '已进入的数量：',
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.2,
                    color: const Color(0xffA0A7B5),
                    fontWeight: FontWeight.w400,
                  ),
                ),
                Expanded(
                    child: Text(
                  item.enteredQuantity.toString(),
                  style: TextStyle(
                    fontSize: 12.sp,
                    height: 1.2,
                    color: const Color(0xff495061),
                    fontWeight: FontWeight.w400,
                  ),
                )),
              ],
            ),
          ]),
        ).clickOnce(() {
          if (widget.selectedAccessControl.any((element) => element.id == item.id)) {
            widget.selectedAccessControl.removeWhere((element) => element.id == item.id);
          } else {
            widget.selectedAccessControl.add(item);
          }
          setState(() {});
        });
      },
    );
  }

  /// 获取列表数据
  Future<List<GateEquipmentEntity>> _getListData(int pageNum, int pageSize, {String? search}) async {
    // 如果是刷新
    if (pageNum == 1) {
      widget.selectedAccessControl.clear();
    }
    final List<String> regionIds = [];
    regionIds.addAll(widget.selectedRegion.map((obj) => obj.id).toList());
    // 获取列表接口
    final res = await HttpUtil().generatePostRequest<List<GateEquipmentEntity>>(
      Interface.getEquipmentListByRegion,
      data: {
        'equipmentName': search,
        'equipmentType': widget.equipmentType, //	设备类型 1:人员闸机 2:车辆闸机
        'regionIds': regionIds,
        // 'regionIds': [],
      },
    ).execute();
    List<GateEquipmentEntity> data = [];
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    // 将id不同的对象添加到选中数组中
    widget.selectedAccessControl.addAll(data.where((character) => !widget.selectedAccessControl.any((obj) => obj.id == character.id)));
    return data;
  }
}
